(() => {

    'use strict';
    goog.provide('Blockly.Blocks.ruilong');
    goog.require('Blockly.Blocks');
    goog.require('path');

    const mediaDirPath = path.join(document.currentScript.src, '../../media/');

    Blockly.Msg['RUILONG_HUE'] = 120;


    //执行器-电机转速
    Blockly.Blocks.ruilong_8833motor = {
        init: function () {
            this.setColour(Blockly.Msg['RUILONG_HUE']);
            //this.appendDummyInput("").appendField("电机控制");
            this.appendValueInput("ID").setCheck(Number).appendField("HR8833驱动器电机控制");
            this.appendValueInput("SPEED").setCheck(Number).appendField("速度-255~255");
            this.setInputsInline(true);
            this.setPreviousStatement(true);
            this.setNextStatement(true);
        }
    };
    //执行器-电机转速
    Blockly.Blocks.ruilong_293motor = {
        init: function () {
            this.setColour(Blockly.Msg['RUILONG_HUE']);
            //this.appendDummyInput("").appendField("电机控制");
            this.appendValueInput("ID").setCheck(Number).appendField("L293驱动器电机控制");
            this.appendValueInput("SPEED").setCheck(Number).appendField("速度-255~255");
            this.setInputsInline(true);
            this.setPreviousStatement(true);
            this.setNextStatement(true);
        }
    };

    Blockly.Blocks.ruilong_I2Cmotor = {
        init: function () {
            this.setColour(Blockly.Msg['RUILONG_HUE']);
            this.appendValueInput("ID").setCheck(Number).appendField("I2C电机控制");
            this.appendValueInput("SPEED").setCheck(Number).appendField("速度-255~255");
            this.setInputsInline(true);
            this.setPreviousStatement(true);
            this.setNextStatement(true);
        }
    };


    // ESP32_Motor
    Blockly.Arduino.motor_id = function() {
      var code = this.getFieldValue('CHANNEL');
      return [code, Blockly.Arduino.ORDER_ATOMIC];
  };

  Blockly.Blocks['motor_id'] = {
    init: function() {
        this.setColour(Blockly.Msg['RUILONG_HUE']);
        this.appendDummyInput("")
        .appendField(new Blockly.FieldDropdown([
            ["1","1"],["2","2"],["3","3"],["4","4"],["5","5"],["6","6"]
         ]), "CHANNEL");
        this.setOutput(true);
   // this.setTooltip(Blockly.MIXLY_TOOLTIP_INOUT_HIGHLOW);
}
};


Blockly.Blocks.ESP32_Motor_Setup= {
  init: function() {
    this.setColour(Blockly.Msg['RUILONG_HUE']);
    this.appendDummyInput("")
    .appendField('ESP32_双PWM电机控制');
    this.appendValueInput('MOTOR_ID')
    .setCheck(Number)
    .appendField(Blockly.MOTOR_HR8833_TEAM_NO);
    this.appendValueInput("PINA")
    .setCheck(Number)
    .setAlign(Blockly.ALIGN_RIGHT)
    .appendField(Blockly.Msg.MIXLY_PIN+"A");
    this.appendValueInput("PINB")
    .setCheck(Number)
    .setAlign(Blockly.ALIGN_RIGHT)
    .appendField(Blockly.Msg.MIXLY_PIN+"B");
    this.setInputsInline(true);
    this.setPreviousStatement(true);
    this.setNextStatement(true);
}
};

Blockly.Blocks.ESP32_Motor_Speed= {
  init: function() {
    this.setColour(Blockly.Msg['RUILONG_HUE']);
    this.appendDummyInput("")
    .appendField('ESP32_双PWM电机控制');
    this.appendValueInput('MOTOR_ID')
    .setCheck(Number)
    .appendField("组号");
    this.appendValueInput('SPEED')
    .setCheck(Number)
    .appendField('速度(-100~100)');
    this.setInputsInline(true);
    this.setPreviousStatement(true);
    this.setNextStatement(true);
}
};

//ESP32旋转编码器
Blockly.Blocks.ESP32_Encoder_getCountA = {
  init: function() {
    this.setColour(Blockly.Msg['RUILONG_HUE']);
    this.appendDummyInput("").appendField("获取ESP32旋转编码器数值");
    this.appendDummyInput("").appendField(new Blockly.FieldDropdown([["单路脉冲","attachSingleEdge"]]), "ENC_MODE");
    this.appendValueInput("PINA", Number).appendField(Blockly.Msg.MIXLY_PIN+"A").setCheck(Number);
    this.setOutput(true, Boolean);
    this.setInputsInline(true);
    this.setTooltip('');
}
};
Blockly.Blocks.ESP32_Encoder_setCountA = {
  init: function() {
    this.setColour(Blockly.Msg['RUILONG_HUE']);
    this.appendDummyInput("").appendField("设置ESP32旋转编码器");
    this.appendDummyInput("").appendField(new Blockly.FieldDropdown([["单路脉冲","attachSingleEdge"]]), "ENC_MODE");
    this.appendValueInput("PINA", Number).appendField(Blockly.Msg.MIXLY_PIN+"A").setCheck(Number);
    this.appendValueInput("VAL").setCheck(Number).appendField('初始数值');
    this.setPreviousStatement(true);
    this.setNextStatement(true);
    this.setInputsInline(true);
    this.setTooltip('');
}
};

Blockly.Blocks.ESP32_Encoder_crpCountA = {
  init: function() {
    this.setColour(Blockly.Msg['RUILONG_HUE']);
    this.appendDummyInput("").appendField("设置ESP32旋转编码器");
    this.appendDummyInput("").appendField(new Blockly.FieldDropdown([["单路脉冲","attachSingleEdge"]]), "ENC_MODE");
    this.appendValueInput("PINA", Number).appendField(Blockly.Msg.MIXLY_PIN+"A").setCheck(Number);
    this.appendDummyInput("").appendField(new Blockly.FieldDropdown([["清除计数器","clearCount"],["恢复计数器","resumeCount"],["暂停计数器","pauseCount"]]), "ACT");
    this.setPreviousStatement(true);
    this.setNextStatement(true);
    this.setInputsInline(true);
    this.setTooltip('');
}
};

Blockly.Blocks.ESP32_Encoder_getCountAB = {
  init: function() {
    this.setColour(Blockly.Msg['RUILONG_HUE']);
    this.appendDummyInput("").appendField("ESP32旋转编码器");
    this.appendDummyInput("").appendField(new Blockly.FieldDropdown([["AB脉冲","attachHalfQuad"],["AB脉冲倍频","attachFullQuad"]]), "ENC_MODE");
    this.appendValueInput("PINA", Number).appendField(Blockly.Msg.MIXLY_PIN+"A").setCheck(Number);
    this.appendValueInput("PINB", Number).appendField(Blockly.Msg.MIXLY_PIN+"B").setCheck(Number);
    this.setOutput(true, Boolean);
    this.setInputsInline(true);
    this.setTooltip('');
}
};

Blockly.Blocks.ESP32_Encoder_setCountAB = {
  init: function() {
    this.setColour(Blockly.Msg['RUILONG_HUE']);
    this.appendDummyInput("").appendField("设置ESP32旋转编码器");
    this.appendDummyInput("").appendField(new Blockly.FieldDropdown([["AB脉冲","attachHalfQuad"],["AB脉冲倍频","attachFullQuad"]]), "ENC_MODE");
    this.appendValueInput("PINA", Number).appendField(Blockly.Msg.MIXLY_PIN+"A").setCheck(Number);
    this.appendValueInput("PINB", Number).appendField(Blockly.Msg.MIXLY_PIN+"B").setCheck(Number);
    this.appendValueInput("VAL").setCheck(Number).appendField('初始数值');
    this.setPreviousStatement(true);
    this.setNextStatement(true);
    this.setInputsInline(true);
    this.setTooltip('');
}
};
Blockly.Blocks.ESP32_Encoder_crpCountAB = {
  init: function() {
    this.setColour(Blockly.Msg['RUILONG_HUE']);
    this.appendDummyInput("").appendField("设置ESP32旋转编码器");
    this.appendDummyInput("").appendField(new Blockly.FieldDropdown([["AB脉冲","attachHalfQuad"],["AB脉冲倍频","attachFullQuad"]]), "ENC_MODE");
    this.appendValueInput("PINA", Number).appendField(Blockly.Msg.MIXLY_PIN+"A").setCheck(Number);
    this.appendValueInput("PINB", Number).appendField(Blockly.Msg.MIXLY_PIN+"B").setCheck(Number);
    this.appendDummyInput("").appendField(new Blockly.FieldDropdown([["清除计数器","clearCount"],["恢复计数器","resumeCount"],["暂停计数器","pauseCount"]]), "ACT");
    this.setPreviousStatement(true);
    this.setNextStatement(true);
    this.setInputsInline(true);
    this.setTooltip('');
}
};


})();